//
//  ELPictureView.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/21.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit

class ELPictureView: UICollectionView {

    // MARK: - 属性
    private let ReuseIdentifier = "StatusPictureViewReuseIdentifier"
    
    var status : ELStatus? {
        didSet {
            self.reloadData()
        }
    }
    
    func caculateViewSize () -> CGSize {
        
        //自定义图片大小
        let itemSize = CGSize(width: 90, height: 90)
        
        //间距
        let margi : CGFloat = 5
        
        layout.itemSize = itemSize
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        //TODO: - 根据数量计算高度
        let count = status?.pic_urls?.count ?? 0
        
        if count == 0 {
            
            return CGSizeZero
        }
        
        //1张图 固定宽高
        if count == 1 {
            
            let Size = CGSize(width: 150, height: 120)
            layout.itemSize = Size
            return Size
        }
        
        //大于一张设置间距
        layout.minimumInteritemSpacing = margi
        layout.minimumLineSpacing = margi
        
        //4张图 2 *图片款度 2*图片高度 + 间距
        if count == 4 {
            return CGSize(width: 2 * itemSize.width + margi, height: 2 * itemSize.height + margi)
        }
        
        //其他情况 根据图片数量计算宽高 最多9张 3列3行
        let colum = 3
        
        //宽度 = 列数 * item宽 + 间隔 * （列数 - 1）
        let width = CGFloat(colum) * itemSize.width + (CGFloat(colum) - 1) * margi
        
        let row : Int = (count + colum - 1) / colum
        
        //高度 = item * 行数 + 间距 * （行数 - 1）
        let height = itemSize.height * CGFloat(row) + CGFloat(colum) * (CGFloat(row) - 1)
        
        return CGSize(width: width, height: height)
    }
    
    
    private var layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        self.registerClass(ELPictureCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        self.dataSource = self
        self.backgroundColor = UIColor.clearColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ELPictureView : UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! ELPictureCell
        
//        status?.pic_urls[indexPath.item][]
        cell.imageURL = status?.storePictureURLs?[indexPath.item]
        
        return cell
    }
}

class ELPictureCell : UICollectionViewCell {
    
    var imageURL : NSURL? {
        didSet {
            imageView.sd_setImageWithURL(imageURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI () {
        
        contentView.addSubview(imageView)
        
        //图片添加约束
        imageView.ff_Fill(contentView)
    }
    
    private lazy var imageView : UIImageView = UIImageView(image: nil)
}